﻿using System;
using System.Drawing;
using System.Windows.Forms;
using PowerManager.Entities;
using PowerManager.Properties;

namespace PowerManager
{
    public class TrayMenuOrganizer
    {
        public EventHandler SchemeChanged;
        private IMainInterface inter;

        public TrayMenuOrganizer(IMainInterface inter)
        {
            this.inter = inter;
        }

        ToolStripMenuItem GetItem(Scheme scheme, bool isActive)
        {
            var item = new ToolStripMenuItem();
            item.Tag = scheme;
            item.Text = string.Format("{0}{1}", isActive ? ">>" : "", scheme.Name);
            item.BackColor = isActive ? 
                Properties.Settings.Default.ActiveSchemeColor : 
                Color.Transparent;
            item.Click += Scheme_Click;
            item.Image = Resources.PicScheme;
            return item;
        }

        public ContextMenuStrip Create()
        {
            // Add the default menu options.
            var menu = new ContextMenuStrip();

            // WinSettings.
            var winSettings = new ToolStripMenuItem();
            winSettings.Text = Resources.BtnWinSettingsMessage;
            winSettings.Click += WinSettings_Click;
            winSettings.Image = Resources.PicPowerOptions;
            winSettings.ImageScaling = ToolStripItemImageScaling.SizeToFit;
            menu.Items.Add(winSettings);

            // Settings.
            var itemSettings = new ToolStripMenuItem();
            itemSettings.Text = Resources.BtnSettingsMessage;
            itemSettings.Click += Settings_Click;
            itemSettings.Image = Resources.PicSettings;
            menu.Items.Add(itemSettings);

            var sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            var schemes = CmdHandler.GetPowerSchemes();
            foreach (var scheme in schemes.AllSchemes)
            {
                menu.Items.Add(GetItem(scheme, schemes.ActiveScheme.Guid == scheme.Guid));
            }

            // Separator.
            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            // Exit.
            var itemExit = new ToolStripMenuItem();
            itemExit.Text = Resources.BtnExitMessage;
            itemExit.Click += Exit_Click;
            itemExit.Image = Resources.PicExit;
            menu.Items.Add(itemExit);

            return menu;
        }

        void Scheme_Click(object sender, EventArgs e)
        {
            var item = sender as ToolStripMenuItem;
            if (item != null)
            {
                var scheme = item.Tag as Scheme;
                if (scheme != null)
                {
                    CmdHandler.SetPowerScheme(scheme.Guid);
                    if (SchemeChanged != null)
                        SchemeChanged(this, new EventArgs());
                }
            }
        }

        void Settings_Click(object sender, EventArgs e)
        {
            inter.OpenSettings();
        }
        void WinSettings_Click(object sender, EventArgs e)
        {
            CmdHandler.StartWinSettings();
        }

        void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
